<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
        <el-form-item label="一级分类">
          <el-select placeholder="请选择一级分类" v-model="category1Id" @change="category1IdChange">
            <el-option v-for="item in cateGory1List" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select placeholder="请选择二级分类" v-model="category2Id" @change="category2IdChange">
            <el-option v-for="item in cateGory2List" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select placeholder="请选择三级分类" v-model="category3Id">
            <el-option v-for="item in cateGory3List" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { ElMessage } from "element-plus";

export default defineComponent({
  name: "Category",
});
</script>

<script setup lang="ts">
import useCateGoryStore from "@/stores/category";
import { onMounted } from "vue";
import { storeToRefs } from "pinia";
const CateGoryStore = useCateGoryStore()

defineProps<{
  disabled: boolean;
}>();
// 获取一级分类的数据
const { cateGory1List, category1Id,cateGory2List,category2Id,cateGory3List,category3Id } = storeToRefs(CateGoryStore)

const category1IdChange = () => {
  cateGory2List.value = [];
  category2Id.value = null;
  cateGory3List.value = [];
  category3Id.value = null;
  //发送2List的请求
  CateGoryStore.getCateGory2List();
}
const category2IdChange = () => {
  //每次当2重选的时候,要把3给清理掉
  cateGory3List.value = [];
  category3Id.value = null;
  //发送2List的请求
  CateGoryStore.getCateGory3List();
};
onMounted(() => {
  try {
    CateGoryStore.getCateGory1List()
  } catch (e: any) {
    ElMessage.error(e.message)
  }
})
</script>